<?php

class Question
{
  public $answers;
  
  public function load($question_id)
  {
    foreach(Db::fetchRow("SELECT * FROM questions WHERE question_id = $question_id") as $key=>$value)
    {
      $this->$key = $value;
    }
    foreach(Db::fetchAll("SELECT * FROM answers WHERE question_id = $question_id") as $answer_data)
    {
      $answer = new Answer();
      foreach($answer_data as $key=>$value)
      {
        $answer->$key = $value;
      }
      $this->answers []= $answer;
    }
    return $this;
  }
  
  public function render()
  {
    $html = '';
    $html .= '<div class="block';
    if($this->sub_answer_id != -1)
    {
      $html .= ' need_answer" id="'.$this->sub_answer_id;
    }
    $html .= '">';
    $html .= '<div class="question-string">'.$this->question_string.'</div>';
    foreach($this->answers as $answer){
      $html .= $answer->render();
    }
    $html .= '</div>';
    return $html;
  }
}
